<template>
    <!-- 对话框表单 -->
    <!-- 建议使用 Prettier 格式化代码 -->
    <!-- el-form 内可以混用 el-form-item、FormItem、ba-input 等输入组件 -->
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :model-value="['Add', 'Edit'].includes(baTable.form.operate!)"
        @close="baTable.toggleForm"
        width="50%"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    v-if="!baTable.form.loading"
                    ref="formRef"
                    @submit.prevent=""
                    @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items"
                    label-position="right"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                >
                    <FormItem :label="t('setup.companies.coyname')" type="string" v-model="baTable.form.items!.coyname" prop="coyname" :placeholder="t('Please input field', { field: t('setup.companies.coyname') })" />
                    <FormItem :label="t('setup.companies.gstno')" type="string" v-model="baTable.form.items!.gstno" prop="gstno" :placeholder="t('Please input field', { field: t('setup.companies.gstno') })" />
                    <FormItem :label="t('setup.companies.companynumber')" type="number" prop="companynumber" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.companynumber" :placeholder="t('Please input field', { field: t('setup.companies.companynumber') })" />
                    <FormItem :label="t('setup.companies.regoffice1')" type="string" v-model="baTable.form.items!.regoffice1" prop="regoffice1" :placeholder="t('Please input field', { field: t('setup.companies.regoffice1') })" />
                    <FormItem :label="t('setup.companies.regoffice2')" type="string" v-model="baTable.form.items!.regoffice2" prop="regoffice2" :placeholder="t('Please input field', { field: t('setup.companies.regoffice2') })" />
                    <FormItem :label="t('setup.companies.regoffice3')" type="string" v-model="baTable.form.items!.regoffice3" prop="regoffice3" :placeholder="t('Please input field', { field: t('setup.companies.regoffice3') })" />
                    <FormItem :label="t('setup.companies.regoffice4')" type="string" v-model="baTable.form.items!.regoffice4" prop="regoffice4" :placeholder="t('Please input field', { field: t('setup.companies.regoffice4') })" />
                    <FormItem :label="t('setup.companies.regoffice5')" type="string" v-model="baTable.form.items!.regoffice5" prop="regoffice5" :placeholder="t('Please input field', { field: t('setup.companies.regoffice5') })" />
                    <FormItem :label="t('setup.companies.regoffice6')" type="string" v-model="baTable.form.items!.regoffice6" prop="regoffice6" :placeholder="t('Please input field', { field: t('setup.companies.regoffice6') })" />
                    <FormItem :label="t('setup.companies.telephone')" type="string" v-model="baTable.form.items!.telephone" prop="telephone" :placeholder="t('Please input field', { field: t('setup.companies.telephone') })" />
                    <FormItem :label="t('setup.companies.fax')" type="string" v-model="baTable.form.items!.fax" prop="fax" :placeholder="t('Please input field', { field: t('setup.companies.fax') })" />
                    <FormItem :label="t('setup.companies.email')" type="string" v-model="baTable.form.items!.email" prop="email" :placeholder="t('Please input field', { field: t('setup.companies.email') })" />
                    <FormItem :label="t('setup.companies.currencydefault')" type="remoteSelect" v-model="baTable.form.items!.currencydefault" prop="currencydefault" :input-attr="{ pk: 'cncerp_fm_currencies.currabrev', field: 'currency', 'remote-url': '/admin/fm.Currencies/index' }" :placeholder="t('Please select field', { field: t('setup.companies.currencydefault') })" />
                    <FormItem :label="t('setup.companies.debtorsact')" type="remoteSelect" v-model="baTable.form.items!.debtorsact" prop="debtorsact" :input-attr="{ pk: 'cncerp_fm_chartmaster.accountcode', field: 'accountname', 'remote-url': '/admin/fm.Chartmaster/index' }" :placeholder="t('Please select field', { field: t('setup.companies.debtorsact') })" />
                    <FormItem :label="t('setup.companies.pytdiscountact')" type="remoteSelect" v-model="baTable.form.items!.pytdiscountact" prop="pytdiscountact" :input-attr="{ pk: 'cncerp_fm_chartmaster.accountcode', field: 'accountcode', 'remote-url': '/admin/fm.Chartmaster/index' }" :placeholder="t('Please select field', { field: t('setup.companies.pytdiscountact') })" />
                    <FormItem :label="t('setup.companies.creditorsact')" type="remoteSelect" v-model="baTable.form.items!.creditorsact" prop="creditorsact" :input-attr="{ pk: 'cncerp_fm_chartmaster.accountcode', field: 'accountcode', 'remote-url': '/admin/fm.Chartmaster/index' }" :placeholder="t('Please select field', { field: t('setup.companies.creditorsact') })" />
                    <FormItem :label="t('setup.companies.payrollact')" type="remoteSelect" v-model="baTable.form.items!.payrollact" prop="payrollact" :input-attr="{ pk: 'cncerp_fm_chartmaster.accountcode', field: 'accountname', 'remote-url': '/admin/fm.Chartmaster/index' }" :placeholder="t('Please select field', { field: t('setup.companies.payrollact') })" />
                    <FormItem :label="t('setup.companies.grnact')" type="remoteSelect" v-model="baTable.form.items!.grnact" prop="grnact" :input-attr="{ pk: 'cncerp_fm_chartmaster.accountcode', field: 'accountcode', 'remote-url': '/admin/fm.Chartmaster/index' }" :placeholder="t('Please select field', { field: t('setup.companies.grnact') })" />
                    <FormItem :label="t('setup.companies.exchangediffact')" type="remoteSelect" v-model="baTable.form.items!.exchangediffact" prop="exchangediffact" :input-attr="{ pk: 'cncerp_fm_chartmaster.accountcode', field: 'accountname', 'remote-url': '/admin/fm.Chartmaster/index' }" :placeholder="t('Please select field', { field: t('setup.companies.exchangediffact') })" />
                    <FormItem :label="t('setup.companies.purchasesexchangediffact')" type="remoteSelect" v-model="baTable.form.items!.purchasesexchangediffact" prop="purchasesexchangediffact" :input-attr="{ pk: 'cncerp_fm_chartmaster.accountcode', field: 'accountname', 'remote-url': '/admin/fm.Chartmaster/index' }" :placeholder="t('Please select field', { field: t('setup.companies.purchasesexchangediffact') })" />
                    <FormItem :label="t('setup.companies.freightact')" type="remoteSelect" v-model="baTable.form.items!.freightact" prop="freightact" :input-attr="{ pk: 'cncerp_fm_chartmaster.accountcode', field: 'accountname', 'remote-url': '/admin/fm.Chartmaster/index' }" :placeholder="t('Please select field', { field: t('setup.companies.freightact') })" />
                    <FormItem :label="t('setup.companies.retainedearnings')" type="remoteSelect" v-model="baTable.form.items!.retainedearnings" prop="retainedearnings" :input-attr="{ pk: 'cncerp_fm_chartmaster.accountcode', field: 'accountname', 'remote-url': '/admin/fm.Chartmaster/index' }" :placeholder="t('Please select field', { field: t('setup.companies.retainedearnings') })" />
                    <FormItem :label="t('setup.companies.gllink_debtors')" type="switch" v-model="baTable.form.items!.gllink_debtors" prop="gllink_debtors" />
                    <FormItem :label="t('setup.companies.gllink_creditors')" type="switch" v-model="baTable.form.items!.gllink_creditors" prop="gllink_creditors" />
                    <FormItem :label="t('setup.companies.gllink_stock')" type="switch" v-model="baTable.form.items!.gllink_stock" prop="gllink_stock" />
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm()">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)" type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref, inject } from 'vue'
import { useI18n } from 'vue-i18n'
import type baTableClass from '/@/utils/baTable'
import FormItem from '/@/components/formItem/index.vue'
import type { FormInstance, FormItemRule } from 'element-plus'
import { buildValidatorData } from '/@/utils/validate'

const formRef = ref<FormInstance>()
const baTable = inject('baTable') as baTableClass

const { t } = useI18n()

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    coycode: [buildValidatorData({ name: 'number', title: t('setup.companies.coycode') })],
    coyname: [buildValidatorData({ name: 'required', title: t('setup.companies.coyname') })],
    gstno: [buildValidatorData({ name: 'required', title: t('setup.companies.gstno') })],
    companynumber: [buildValidatorData({ name: 'required', title: t('setup.companies.companynumber') })],
    currencydefault: [buildValidatorData({ name: 'required', title: t('setup.companies.currencydefault') })],
    debtorsact: [buildValidatorData({ name: 'required', title: t('setup.companies.debtorsact') })],
    creditorsact: [buildValidatorData({ name: 'required', title: t('setup.companies.creditorsact') })],
    payrollact: [buildValidatorData({ name: 'required', title: t('setup.companies.payrollact') })],
    grnact: [buildValidatorData({ name: 'required', title: t('setup.companies.grnact') })],
    exchangediffact: [buildValidatorData({ name: 'required', title: t('setup.companies.exchangediffact') })],
    purchasesexchangediffact: [buildValidatorData({ name: 'required', title: t('setup.companies.purchasesexchangediffact') })],
    freightact: [buildValidatorData({ name: 'required', title: t('setup.companies.freightact') })],
    retainedearnings: [buildValidatorData({ name: 'required', title: t('setup.companies.retainedearnings') })],
    gllink_debtors: [buildValidatorData({ name: 'required', title: t('setup.companies.gllink_debtors') })],
    gllink_creditors: [buildValidatorData({ name: 'required', title: t('setup.companies.gllink_creditors') })],
    gllink_stock: [buildValidatorData({ name: 'required', title: t('setup.companies.gllink_stock') })],
})
</script>

<style scoped lang="scss"></style>
